﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace NimbleTextKeygen
{
    public static class Persister
    {
        private static void Archive(string xmlFileName, string reason)
        {
            string directoryName = Path.GetDirectoryName(xmlFileName);
            string[] str = new string[] { reason, "_", null, null, null };
            str[2] = DateTime.Now.ToString("yyyyMMddHHmmss");
            str[3] = "_";
            str[4] = Path.GetFileName(xmlFileName);
            string str1 = Path.Combine(directoryName, string.Concat(str));
            File.Copy(xmlFileName, str1);
        }

        public static T Clone<T>(T source)
        {
            T t;
            if (source == null)
            {
                t = default(T);
                return t;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            Stream memoryStream = new MemoryStream();
            using (StreamWriter streamWriter = new StreamWriter(memoryStream))
            {
                xmlSerializer.Serialize(streamWriter, source);
                memoryStream.Position = (long)0;
                using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream))
                {
                    try
                    {
                        t = (T)xmlSerializer.Deserialize(xmlTextReader);
                    }
                    catch (Exception exception)
                    {
                        throw;
                    }
                }
            }
            return t;
        }

        public static object LoadObjectFromXMLFile(string xmlFileName, Type type)
        {
            object obj;
            IOUtils.EnsurePathExists(Path.GetDirectoryName(xmlFileName));
            if (!File.Exists(xmlFileName))
            {
                throw new FileNotFoundException(string.Concat("File Not Found, hence can't deserialize into ", type.ToString()), xmlFileName);
            }
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            using (FileStream fileStream = new FileStream(xmlFileName, FileMode.Open))
            {
                using (XmlTextReader xmlTextReader = new XmlTextReader(fileStream))
                {
                    try
                    {
                        obj = xmlSerializer.Deserialize(xmlTextReader);
                    }
                    catch (Exception exception)
                    {
                        Persister.Archive(xmlFileName, "failed");
                        Log.WriteLine(exception, string.Concat("Error loading file '", xmlFileName, "'"));
                        obj = null;
                    }
                }
            }
            return obj;
        }

        public static void SaveObjectToXmlFile(string fileName, object obj)
        {
            try
            {
                IOUtils.EnsurePathExists(Path.GetDirectoryName(fileName));
                XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
                using (StreamWriter streamWriter = new StreamWriter(fileName))
                {
                    xmlSerializer.Serialize(streamWriter, obj);
                }
            }
            catch (Exception exception)
            {
                Log.WriteLine(exception, string.Concat("Error while saving file to object. file:'", fileName, "'"));
                throw;
            }
        }
    }
}
